/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef TRI_ENTITY_H_
#define TRI_ENTITY_H_

#include <string>
#include <set>

#include "material.h"
#include "transform.h"
#include "render.h"
#include "events.h"


namespace tri
{


class TScene;
class TEntity;

class TAbstractEntityParent{
public:
	TAbstractEntityParent();
	virtual ~TAbstractEntityParent();
	
	virtual void removeAll()=0;
	virtual void removeEntity(TEntity*)=0;
	virtual void addEntity(TEntity*)=0;
	virtual bool hasEntity(TEntity* child)=0;
	virtual void getEntitySet(std::set<TEntity*>& EntSet)=0;
	virtual TEntity* getEntity(unsigned int uid)=0;
};

class TEntity : public TTransform, 
				public TAbstractRenderer, 
				public TAbstractEntityParent,
				public TAbstractEventComponent{
	
	friend class TScene;
	
protected:
	typedef std::set<TEntity*> TEntitySet;
	typedef std::set<TEntity*>::const_iterator ConstIterator;
	
public:
	TEntity();
	virtual ~TEntity();
	
	unsigned int getUID() const;

	virtual void display()=0;
	virtual void update();
	virtual void render();
	virtual void renderChildren();
	
	//parent/child interface
	virtual void removeEntity(TEntity* child);
	virtual void addEntity(TEntity* child);
	virtual void removeAll();
	virtual bool hasEntity(TEntity* child);
	virtual void getEntitySet(std::set<TEntity*>& EntSet);
	TAbstractEntityParent* getParent();
	void setParent(TAbstractEntityParent*);
	virtual TEntity* getEntity(unsigned int uid);
	
	//material and texture methods
		
	void setColor(int r, int g, int b, int a=255);
	void getColor(int& r, int& g, int& b, int& a);
	void setTargetColor(int r, int g, int b, int a=255, float t = 1.0);
	void applyColor();
	void setColor(graphics::RGBA);
	void setTargetColor(graphics::RGBA,float t = 1.0);
	tri::graphics::TColor getColor();
	tri::graphics::RGBA getColorAsRGBA();
	
	void setImage(std::string image,bool filter=false,bool mipmap=false);
	std::string getImage();
	bool isImageFiltered();
	bool isImageMipmap();
	tri::TTexture* getTexture();
	
	void setMaterial(std::string);
	std::string getMaterial();
	TMaterial* getMaterialPointer();
	
	void setVisible(bool b);
	bool isVisible();
	
	void setUIDLoad(bool b);
	
private:
	TAbstractEntityParent* m_Parent;
	TEntitySet m_EntitySet;
	TMaterial mMaterial;
	std::string mMaterialPointer;
	unsigned int m_EntityUID;
	bool m_LoadUID;
	bool m_isVisible;
	
	static unsigned int s_EntityUIDCount;
};

}

#endif /*TRI_ENTITY_H_*/
